﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace KSS.Generator.Configuration
{
    public class ConfigurationHelper
    {
        public static Type GetDataType(string dataType)
        {
            string assemblyName = string.Empty;
            string className = string.Empty;
            PropertyDataType eDataType;

            try
            {
                eDataType = (PropertyDataType)Enum.Parse(typeof(PropertyDataType), dataType, true);
            }
            catch
            {
                return GetDataType(string.Empty, dataType);
            }

            switch (eDataType)
            {
                case PropertyDataType.String:
                    assemblyName = "mscorlib";
                    className = "System.String";
                    break;
                case PropertyDataType.Number:
                    assemblyName = "mscorlib";
                    className = "System.Int32";
                    break;
                case PropertyDataType.Boolean:
                    assemblyName = "mscorlib";
                    className = "System.Boolean";
                    break;
                case PropertyDataType.Color:
                    assemblyName = "System.Drawing";
                    className = "System.Drawing.Color";
                    break;
                case PropertyDataType.Font:
                    assemblyName = "System.Drawing";
                    className = "System.Drawing.Font";
                    break;
                case PropertyDataType.Size:
                    assemblyName = "System.Drawing";
                    className = "System.Drawing.Size";
                    break;
                case PropertyDataType.Image:
                    assemblyName = "System.Drawing";
                    className = "System.Drawing.Image";
                    break;
                default:
                    //throw new Exception("Property type " + eDataType.ToString() + " not yet be supported");
                    break;
            }

            if (assemblyName == string.Empty)
            {
                return null;
            }

            Assembly asm = Assembly.Load(assemblyName);

            return asm.GetType(className);
        }

        public static Type GetDataType(string assemblyName, string className)
        {
            Assembly asm = null;

            if (string.IsNullOrEmpty(assemblyName))
            {
                asm = Assembly.GetExecutingAssembly();
            }
            else
            {
                asm = Assembly.Load(assemblyName);
            }

            return asm.GetType(asm.FullName + "." + className);
        }

        public static List<T> RemoveDuplicateSections<T>(List<T> sections) where T : PropertySetting
        {
            Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
            List<T> finalList = new List<T>();
            int i = 0;
            foreach (T currValue in sections)
            {
                if (uniqueStore.ContainsKey(currValue.Name) == false)
                {
                    uniqueStore.Add(currValue.Name, 0);
                    finalList.Add(sections[i]);
                }
                i++;
            }
            return finalList;
        }
    }
}
